package com.example.simplenet;

import java.util.Timer;
import java.util.TimerTask;

import com.example.simplenet.ConnectionMan.Mission;
import com.example.simplenet.ConnectionMan.MissionType;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MissionsFragment extends MyFragment {
	
	private ListView mListView = null;
	private MissionsAdapter mAdapter = null;
	private Timer mTimer = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.missions_fragment, container,
				false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		mListView = (ListView) getView().findViewById(R.id.missionsList);
		mAdapter = new MissionsAdapter();
		mListView.setAdapter(mAdapter);
		updateMissions();
		super.onActivityCreated(savedInstanceState);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		mTimer = new Timer();
		mTimer.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						mAdapter.notifyDataSetChanged();
					}
				});
			}
		}, 0, 1000);
	}
	
	@Override
	public void onPause() {
		if (mTimer != null)
			mTimer.cancel();
		super.onPause();
	}
	
	private class MissionsAdapter extends ArrayAdapter<Mission> {
		
		private ViewHolder mViewHolder;
		
		public MissionsAdapter() {
			super(getActivity(), R.layout.mission_row);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				LayoutInflater inflater = (LayoutInflater) getContext()
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.mission_row, parent, false);
				
				mViewHolder = new ViewHolder();
				mViewHolder.mAttacker = (TextView) convertView.findViewById(R.id.attacker_name);
				mViewHolder.mDestination = (TextView) convertView.findViewById(R.id.attack_destination);
				mViewHolder.mType = (TextView) convertView.findViewById(R.id.attack_type);
				mViewHolder.mTime = (TextView) convertView.findViewById(R.id.time_left);
				
				convertView.setTag(mViewHolder);
			} else {
				mViewHolder = (ViewHolder) convertView.getTag();
			}
			
			Mission mission = getItem(position);
			boolean myMission = mission.getType() == MissionType.MY;
			String timeLeft = mission.getTimeLeft();

//			if (timeLeft.equals(""))
//				convertView.setVisibility(View.GONE);
//			else {
				convertView.setVisibility(View.VISIBLE);
				mViewHolder.mTime.setText(timeLeft);
				if (mission.isReturn()) { 
					mViewHolder.mAttacker.setText(myMission ? mission.getOtherName() + " " + mission.getHisProvince() : mission.getMyProvince());
					mViewHolder.mDestination.setText(myMission ? mission.getMyProvince() : mission.getOtherName() + " " + mission.getHisProvince());
				} else {
					mViewHolder.mAttacker.setText(myMission ? mission.getMyProvince() : mission.getOtherName() + " " + mission.getHisProvince());
					mViewHolder.mDestination.setText(myMission ? mission.getOtherName() + " " + mission.getHisProvince() : mission.getMyProvince() + " " + mission.getMyProvince());
				}
				mViewHolder.mType.setText(mission.getSubTypeString());
//			}
			
			return convertView;
		}
		
		private class ViewHolder {
			TextView mAttacker;
			TextView mDestination;
			TextView mType;
			TextView mTime;
		}
		
	}
	
	public void updateMissions() {
		mAdapter.clear();
		mAdapter.addAll(ArmyController.getAllMissions());
		mAdapter.notifyDataSetChanged();
	}
	
}
